{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码2-1 创建数组并查看数组属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 创建的数组为：  [1 2 3 4]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np  # 导入NumPy库 \n",
    "arr1 = np.array([1, 2, 3, 4])  # 创建一维数组 \n",
    "print(' 创建的数组为： ', arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的数组为：\n",
      " [[ 1  2  3  4]\n",
      " [ 4  5  6  7]\n",
      " [ 7  8  9 10]]\n"
     ]
    }
   ],
   "source": [
    "# 创建二维数组\n",
    "arr2 = np.array([[1, 2, 3, 4],[4, 5, 6, 7], [7, 8, 9, 10]])\n",
    "print('创建的数组为：\\n', arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组维度为： (3, 4)\n",
      "数组类型为： int32\n",
      "数组元素个数为： 12\n",
      "数组每个元素大小为： 4\n"
     ]
    }
   ],
   "source": [
    "print('数组维度为：', arr2.shape)  # 查看数组结构\n",
    "print('数组类型为：', arr2.dtype)  # 查看数组类型\n",
    "print('数组元素个数为：', arr2.size)  # 查看数组元素个数\n",
    "print('数组每个元素大小为：', arr2.itemsize)  # 查看数组每个元素大小"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码2-2 重新设置数组的shape属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "重新设置shape 后的arr2 为：\n",
      " [[ 1  2  3]\n",
      " [ 4  4  5]\n",
      " [ 6  7  7]\n",
      " [ 8  9 10]]\n"
     ]
    }
   ],
   "source": [
    "arr2.shape = 4,3  # 重新设置shape\n",
    "print('重新设置shape 后的arr2 为：\\n', arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码2-3 使用arange函数创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用arange函数创建的数组为：\n",
      " [0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]\n"
     ]
    }
   ],
   "source": [
    "print('使用arange函数创建的数组为：\\n', np.arange(0,1,0.1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码2-4 使用linespace函数创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用linspace函数创建的数组为：\n",
      " [0.         0.09090909 0.18181818 0.27272727 0.36363636 0.45454545\n",
      " 0.54545455 0.63636364 0.72727273 0.81818182 0.90909091 1.        ]\n"
     ]
    }
   ],
   "source": [
    "print('使用linspace函数创建的数组为：\\n', np.linspace(0, 1, 12))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码2-5 使用logspace函数创建等比数列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用logspace函数创建的数组为：\n",
      " [  1.           1.27427499   1.62377674   2.06913808   2.6366509\n",
      "   3.35981829   4.2813324    5.45559478   6.95192796   8.8586679\n",
      "  11.28837892  14.38449888  18.32980711  23.35721469  29.76351442\n",
      "  37.92690191  48.32930239  61.58482111  78.47599704 100.        ]\n"
     ]
    }
   ],
   "source": [
    "print('使用logspace函数创建的数组为：\\n', np.logspace(0, 2, 20))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码2-6 使用zeros函数创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用zeros函数创建的数组为：\n",
      " [[0. 0. 0.]\n",
      " [0. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "print('使用zeros函数创建的数组为：\\n', np.zeros((2,3)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码2-7 使用eye函数创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用eye函数创建的数组为：\n",
      " [[1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]]\n"
     ]
    }
   ],
   "source": [
    "print('使用eye函数创建的数组为：\\n', np.eye(3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码2-8 使用diag函数创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用diag函数创建的数组为：\n",
      " [[1 0 0 0]\n",
      " [0 2 0 0]\n",
      " [0 0 3 0]\n",
      " [0 0 0 4]]\n"
     ]
    }
   ],
   "source": [
    "print('使用diag函数创建的数组为：\\n', np.diag([1,2,3,4]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码2-9 使用ones函数创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用ones函数的数组为：\n",
      " [[1. 1. 1.]\n",
      " [1. 1. 1.]\n",
      " [1. 1. 1.]\n",
      " [1. 1. 1.]\n",
      " [1. 1. 1.]]\n"
     ]
    }
   ],
   "source": [
    "print('使用ones函数的数组为：\\n', np.ones((5,3)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码2-10 数组的数据类型转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "转换结果为： 42.0\n",
      "转换结果为： 42\n",
      "转换结果为： True\n",
      "转换结果为： False\n",
      "转换结果为： 1.0\n",
      "转换结果为： 0.0\n"
     ]
    }
   ],
   "source": [
    "print('转换结果为：', np.float64(42))  # 整型转换为浮点型\n",
    "print('转换结果为：', np.int8(42.0))  # 浮点型转换为整型\n",
    "print('转换结果为：', np.bool_(42))  # 整型转换为布尔型\n",
    "print('转换结果为：', np.bool_(0))  # 整型转换为布尔型\n",
    "print('转换结果为：', np.float_(True))  # 布尔型转换为浮点型\n",
    "print('转换结果为：', np.float_(False))  # 布尔型转换为浮点型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码2-11 创建数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据类型为：\n",
      " [('name', '<U40'), ('numitems', '<i8'), ('price', '<f8')]\n"
     ]
    }
   ],
   "source": [
    "df = np.dtype([('name', np.str_, 40), ('numitems', np.int64),\n",
    "    ('price',np.float64)])\n",
    "print('数据类型为：\\n', df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码2-12 查看数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据类型为： <U40\n",
      "数据类型为： <U40\n"
     ]
    }
   ],
   "source": [
    "print('数据类型为：', df['name'])\n",
    "print('数据类型为：', np.dtype(df['name']))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码2-13 自定义数组数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "自定义数据为： [('tomatoes', 42, 4.14) ('cabbages', 13, 1.72)]\n"
     ]
    }
   ],
   "source": [
    "itemz = np.array([('tomatoes', 42, 4.14), ('cabbages', 13, 1.72)],\n",
    "    dtype=df)\n",
    "print('自定义数据为：', itemz)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码2-14 使用random函数生成随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成的随机数组为：\n",
      " [0.38145352 0.89084352 0.49846437 0.54677638 0.737858   0.60604355\n",
      " 0.79509118 0.88859328 0.80278933 0.46629197 0.9078445  0.04541739\n",
      " 0.21169787 0.39919589 0.99753722 0.04491506 0.7056074  0.49606675\n",
      " 0.31582016 0.44302747 0.50648099 0.20597387 0.61864355 0.86750187\n",
      " 0.84830638 0.46709575 0.77923201 0.40805243 0.32125816 0.0553435\n",
      " 0.56472445 0.36290651 0.33089927 0.66085181 0.70388842 0.27492189\n",
      " 0.01841877 0.28655585 0.96518589 0.86816549 0.00114747 0.66591494\n",
      " 0.16228597 0.82872113 0.80593881 0.28760537 0.77574314 0.59800532\n",
      " 0.97414927 0.12737811 0.84095489 0.2251041  0.37665578 0.11480771\n",
      " 0.64330205 0.91484059 0.85836131 0.80310152 0.34144149 0.40961967\n",
      " 0.68148243 0.31099037 0.28618753 0.85745721 0.48949236 0.98318963\n",
      " 0.40066294 0.16816847 0.36845301 0.88170877 0.1476515  0.55696417\n",
      " 0.67506738 0.60280495 0.33229359 0.59860729 0.43025339 0.764076\n",
      " 0.07466126 0.17494425 0.07416306 0.15629328 0.33318312 0.8302605\n",
      " 0.49368231 0.1092517  0.57509576 0.45892231 0.85420212 0.33607747\n",
      " 0.81233547 0.21993048 0.74326126 0.41986644 0.35814048 0.48544472\n",
      " 0.00324658 0.62470395 0.47434787 0.48940868]\n",
      "生成0-100的随机数组：\n",
      " [ 3.695775   21.56716646 72.15517844 66.96616423 46.52269219 52.23692075\n",
      " 88.7119412  12.06562339 73.92993033 19.47612168]\n"
     ]
    }
   ],
   "source": [
    "print('生成的随机数组为：\\n', np.random.random(100))\n",
    "print('生成0-100的随机数组：\\n', np.random.random(10) * 100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码 2-15 使用rand函数生成均匀分布的随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成的随机数组为：\n",
      " [[0.7065114  0.11080708 0.0914436  0.69744845 0.35577678]\n",
      " [0.30331199 0.49531674 0.46801387 0.04931301 0.80418348]\n",
      " [0.90371779 0.92604521 0.91292547 0.00703931 0.51499624]\n",
      " [0.88151456 0.50079282 0.13262926 0.63964505 0.36907462]\n",
      " [0.74134203 0.33635768 0.98856678 0.21623421 0.84693736]\n",
      " [0.06845465 0.03901343 0.98781425 0.26351746 0.06584649]\n",
      " [0.82834571 0.28808327 0.50933576 0.94921373 0.35441865]\n",
      " [0.78348675 0.80786195 0.30290478 0.78467501 0.97216773]\n",
      " [0.62810918 0.72282724 0.34076713 0.71692399 0.61106   ]\n",
      " [0.17565104 0.8836084  0.40095075 0.3954952  0.30245549]]\n"
     ]
    }
   ],
   "source": [
    "print('生成的随机数组为：\\n', np.random.rand(10,5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码 2-16 使用randn函数生成服从正态分布的随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成的随机数组为：\n",
      " [[ 0.11045296 -1.21331247 -1.80478285 -0.05814929  1.17306871]\n",
      " [-0.3264573  -0.28791948  0.26115141 -2.40390234 -1.13244542]\n",
      " [ 0.60089901 -0.94658823 -0.30723082 -0.02670047  0.39895744]\n",
      " [ 1.60175129 -1.11779416  0.61588682 -0.47512864  0.13116479]\n",
      " [ 0.93835728  0.91908542  2.11937716  0.90562455  1.16784917]\n",
      " [ 0.34696465 -1.13702228 -1.37887181 -0.53757866  1.11739713]\n",
      " [ 0.25217096 -0.32085842 -0.14425363  0.01784868 -0.37633342]\n",
      " [-0.94660609 -1.40854669 -1.71375163 -0.56710474  0.49950305]\n",
      " [-0.39387552  0.30625671  0.995557    1.00880352 -1.06253956]\n",
      " [-1.58976436  0.25191027 -1.39520359  0.31852878  0.22221268]]\n"
     ]
    }
   ],
   "source": [
    "print('生成的随机数组为：\\n', np.random.randn(10,5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码 2-17 使用randint函数生成指定范围的随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成的随机数组为：\n",
      " [[3 2 6 7 7]\n",
      " [2 3 6 8 5]]\n"
     ]
    }
   ],
   "source": [
    "print('生成的随机数组为：\\n', np.random.randint(2, 10, size=[2,5]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码 2-18 使用索引访问一维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为： 5\n",
      "索引结果为： [3 4]\n",
      "索引结果为： [0 1 2 3 4]\n",
      "索引结果为： 9\n",
      "索引结果为： [  0   1 100 101   4   5   6   7   8   9]\n",
      "索引结果为： [  1 101   5   7]\n",
      "索引结果为： [  5 101]\n"
     ]
    }
   ],
   "source": [
    "arr = np.arange(10)\n",
    "print('索引结果为：', arr[5])  # 用整数作为下标可以获取数组中的某个元素\n",
    "# 用范围作为下标获取数组的一个切片，包括arr[3]不包括arr[5]\n",
    "print('索引结果为：', arr[3:5])\n",
    "print('索引结果为：', arr[:5])  # 省略开始下标，表示从arr[0]开始\n",
    "# 下标可以使用负数，-1表示从数组后往前数的第一个元素\n",
    "print('索引结果为：', arr[-1]) # 索引最后一个元素\n",
    "\n",
    "arr[2:4] = 100,101\n",
    "print('索引结果为：', arr)  # 下标还可以用来修改元素的值\n",
    "# 范围中的第三个参数表示步长，2表示隔一个元素取一个元素\n",
    "print('索引结果为：', arr[1:-1:2])\n",
    "print('索引结果为：', arr[5:1:-2])  # 步长为负数时，开始下标必须大于结束下标"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码 2-19 使用索引访问多维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的二维数组为：\n",
      " [[ 1  2  3  4  5]\n",
      " [ 4  5  6  7  8]\n",
      " [ 7  8  9 10 11]]\n",
      "索引结果为： [4 5]\n",
      "索引结果为：\n",
      " [[ 6  7  8]\n",
      " [ 9 10 11]]\n",
      "索引结果为： [3 6 9]\n",
      "索引第1行中第一列和第二列：\n",
      " [4 5]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([[1, 2, 3, 4, 5],[4, 5, 6, 7, 8], [7, 8, 9, 10, 11]])\n",
    "print('创建的二维数组为：\\n', arr)\n",
    "print('索引结果为：', arr[0,3:5])  # 索引第0行中第3和第4列的元素\n",
    "# 索引第2和第3行中第3列、第4列和第5列的元素\n",
    "print('索引结果为：\\n', arr[1:,2:])\n",
    "print('索引结果为：', arr[:,2])  # 索引第2列的元素\n",
    "print('索引第1行中第一列和第二列：\\n', arr[1, 0:2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码 2-20 使用整数序列索引和布尔值索引访问多维数组 (待解决)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为： [ 2  6 10]\n",
      "索引结果为： [3 9]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([[1, 2, 3, 4, 5],[4, 5, 6, 7, 8], [7, 8, 9, 10, 11]])\n",
    "# 从两个序列的对应位置取出两个整数组成下标：arr[0,1], arr[1,2], arr[2,3]\n",
    "print('索引结果为：', arr[(0, 1, 2), (1, 2, 3)])\n",
    "# print('索引结果为：', arr[1:, (0, 2, 3)])  # 索引第2、3行中第0、2、3列的元素\n",
    "\n",
    "mask = np.array([1, 0, 1], dtype = np.bool_)\n",
    "# mask是一个布尔数组，它索引第1、3行中第2列的元素\n",
    "print('索引结果为：', arr[mask, 2])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码 2-21 改变数组形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的一维数组为： [ 0  1  2  3  4  5  6  7  8  9 10 11]\n",
      "新的一维数组为：\n",
      " [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "数组维度为： 2\n"
     ]
    }
   ],
   "source": [
    "arr = np.arange(12)  # 创建一维数组\n",
    "print('创建的一维数组为：', arr)\n",
    "print('新的一维数组为：\\n', arr.reshape(3, 4))  # 设置数组的形状\n",
    "print('数组维度为：', arr.reshape(3, 4).ndim)  # 查看数组维度"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码 2-22 使用ravel函数展平数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的二维数组为：\n",
      " [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "数组展平后为： [ 0  1  2  3  4  5  6  7  8  9 10 11]\n"
     ]
    }
   ],
   "source": [
    "arr = np.arange(12).reshape(3, 4)\n",
    "print('创建的二维数组为：\\n', arr)\n",
    "print('数组展平后为：', arr.ravel())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码 2-23 使用flatten函数展平数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组展平为： [ 0  1  2  3  4  5  6  7  8  9 10 11]\n",
      "数组展平为： [ 0  4  8  1  5  9  2  6 10  3  7 11]\n"
     ]
    }
   ],
   "source": [
    "print('数组展平为：', arr.flatten())  # 横向展平\n",
    "print('数组展平为：', arr.flatten('F'))  # 纵向展平"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码 2-24 使用hstack函数实现数组横向组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的数组1为：\n",
      " [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "创建的数组2为：\n",
      " [[ 0  3  6  9]\n",
      " [12 15 18 21]\n",
      " [24 27 30 33]]\n",
      "横向组合为：\n",
      " [[ 0  1  2  3  0  3  6  9]\n",
      " [ 4  5  6  7 12 15 18 21]\n",
      " [ 8  9 10 11 24 27 30 33]]\n"
     ]
    }
   ],
   "source": [
    "arr1 = np.arange(12).reshape(3, 4)\n",
    "print('创建的数组1为：\\n', arr1)\n",
    "\n",
    "arr2 = arr1*3\n",
    "print('创建的数组2为：\\n', arr2)\n",
    "print('横向组合为：\\n', np.hstack((arr1, arr2)))  # hstack函数横向组合"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码 2-25 使用vstack函数实现数组纵向组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "纵向组合为：\n",
      " [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [ 0  3  6  9]\n",
      " [12 15 18 21]\n",
      " [24 27 30 33]]\n"
     ]
    }
   ],
   "source": [
    "print('纵向组合为：\\n', np.vstack((arr1, arr2)))  # vstack函数纵向组合"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码 2-26 使用concatenate函数组合数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "横向组合为：\n",
      " [[ 0  1  2  3  0  3  6  9]\n",
      " [ 4  5  6  7 12 15 18 21]\n",
      " [ 8  9 10 11 24 27 30 33]]\n",
      "纵向组合为：\n",
      " [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [ 0  3  6  9]\n",
      " [12 15 18 21]\n",
      " [24 27 30 33]]\n"
     ]
    }
   ],
   "source": [
    "print('横向组合为：\\n', np.concatenate((arr1, arr2), axis=1))  # concatenate函数横向组合\n",
    "print('纵向组合为：\\n', np.concatenate((arr1, arr2), axis=0))  # concatenate函数纵向组合"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码 2-27 使用hsplit函数实现数组横向分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的二维数组为：\n",
      " [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]]\n",
      "横向分割为：\n",
      " [array([[ 0,  1],\n",
      "       [ 4,  5],\n",
      "       [ 8,  9],\n",
      "       [12, 13]]), array([[ 2,  3],\n",
      "       [ 6,  7],\n",
      "       [10, 11],\n",
      "       [14, 15]])]\n"
     ]
    }
   ],
   "source": [
    "arr = np.arange(16).reshape(4, 4)\n",
    "print('创建的二维数组为：\\n', arr)\n",
    "print('横向分割为：\\n', np.hsplit(arr, 2))  # hsplit函数横向分割"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码 2-28 使用vsplit函数实现数组纵向分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "纵向分割为：\n",
      " [array([[0, 1, 2, 3],\n",
      "       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],\n",
      "       [12, 13, 14, 15]])]\n"
     ]
    }
   ],
   "source": [
    "print('纵向分割为：\\n', np.vsplit(arr, 2))  # vsplit函数纵向分割"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码 2-29 使用split函数分割数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "横向分割为：\n",
      " [array([[ 0,  1],\n",
      "       [ 4,  5],\n",
      "       [ 8,  9],\n",
      "       [12, 13]]), array([[ 2,  3],\n",
      "       [ 6,  7],\n",
      "       [10, 11],\n",
      "       [14, 15]])]\n",
      "纵向分割为：\n",
      " [array([[0, 1, 2, 3],\n",
      "       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],\n",
      "       [12, 13, 14, 15]])]\n"
     ]
    }
   ],
   "source": [
    "print('横向分割为：\\n', np.split(arr, 2, axis=1))  # split函数横向分割\n",
    "print('纵向分割为：\\n', np.split(arr, 2, axis=0))  # split函数纵向分割"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
